package cn.iocoder.yudao.module.system.service.a_audio_and_video_log;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.a_manuscriptLog.vo.AudioAndVideoLogPageReqVO;
import cn.iocoder.yudao.module.system.config.a_gaojian.ConnectionConfig;
import com.trs.hybase.client.*;
import com.trs.hybase.client.params.SearchParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lxj
 */
@Service
@Validated
public class AudioAndVideoLogServiceImpl implements AudioAndVideoLogService {

    /**
     * 稿件日志表名
     */
    @Value("${hybase.dbname.audioAndVideoLog}")
    private String dbName;

    @Override
    public Boolean findExistByRelevanceId(TRSConnection conn, String relevanceId) {
        //设置返回结果字段
        TRSResultSet resultSet;
        String whereStr = "relevanceId:" + relevanceId;
        try {
            resultSet = conn.executeSelect(dbName, whereStr, 0, 1, new SearchParams());
            if (resultSet != null) {
                if (resultSet.getNumFound() > 0) {
                    return true;
                }
            }
        } catch (TRSException e) {
            System.out.println("ErrorCode:" + e.getErrorCode());
            System.out.println("ErrorString:" + e.getErrorString());
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return false;
    }

    @Override
    public PageResult<AudioAndVideoLogPageReqVO> findPageByRelevanceId(AudioAndVideoLogPageReqVO pageReqVO) {
        PageResult<AudioAndVideoLogPageReqVO> pageResult = new PageResult<>();
        List<AudioAndVideoLogPageReqVO> audioAndVideoLogPageReqVOList = new ArrayList<>();
        //创建与服务器的连接实例
        TRSConnection conn = ConnectionConfig.getTRSConnection();
        SearchParams param = new SearchParams();
        //设置结果排序方式为创建时间排序
        param.setSortMethod("+version");
        //设置返回结果字段
        param.setReadColumns("id;title;introduction;type;video_resolution;video_cover_url;suffix;duration;create_user_id;create_time;version;url;fileName;deleted;shooting_time;category;relevanceId");

        TRSResultSet resultSet;
        String whereStr = "relevanceId:" + pageReqVO.getRelevanceId();

        try {
            //在"pictures"数据库里检索
            resultSet = conn.executeSelect(dbName, whereStr, ((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize()), pageReqVO.getPageSize(), param);
            if (resultSet != null) {
                for (int i = 0; i < resultSet.size(); i++) {
                    resultSet.moveNext();
                    TRSRecord re = resultSet.get();
                    AudioAndVideoLogPageReqVO audioAndVideoPageReqVO = new AudioAndVideoLogPageReqVO();
                    audioAndVideoPageReqVO.setId(re.getString("id"));
                    audioAndVideoPageReqVO.setTitle(re.getString("title"));
                    audioAndVideoPageReqVO.setIntroduction(re.getString("introduction"));
                    audioAndVideoPageReqVO.setType(re.getInt("type"));
                    audioAndVideoPageReqVO.setVideoResolution(re.getString("video_resolution"));
                    audioAndVideoPageReqVO.setVideoCoverUrl(re.getString("video_cover_url"));
                    audioAndVideoPageReqVO.setSuffix(re.getString("suffix"));
                    audioAndVideoPageReqVO.setDuration(re.getString("duration"));
                    audioAndVideoPageReqVO.setCreateUserId(re.getString("create_user_id"));
                    audioAndVideoPageReqVO.setVersion(re.getInt("version"));
                    audioAndVideoPageReqVO.setCreateTime(re.getString("create_time"));
                    audioAndVideoPageReqVO.setLastModificationTime(re.getString("last_modification_time"));
                    audioAndVideoPageReqVO.setUrl(re.getString("url"));
                    audioAndVideoPageReqVO.setFileName(re.getString("fileName"));
                    audioAndVideoPageReqVO.setShootingTime(re.getString("shooting_time"));
                    audioAndVideoPageReqVO.setCategory(re.getString("category"));
                    audioAndVideoPageReqVO.setRelevanceId(re.getString("relevanceId"));
                    audioAndVideoLogPageReqVOList.add(audioAndVideoPageReqVO);
                }

                pageResult.setTotal(resultSet.getNumFound());
                pageResult.setList(audioAndVideoLogPageReqVOList);

            }
        } catch (TRSException e) {
            System.out.println("ErrorCode:" + e.getErrorCode());
            System.out.println("ErrorString:" + e.getErrorString());
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return pageResult;
    }


}
